import { isLogin, getToken } from 'utils'
import { Toast } from 'vant'
import { addCart } from 'api'

export default {
  namespaced: true,
  state: {
    Homethis: this
  },
  mutations: {
    click_Detail (state, id) {
      state.Homethis.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    add_Cart (state, { id, num }) {
      if (!isLogin()) {
        Toast.fail({
          message: '请登录',
          duration: 1000,
          onClose: () => {
            state.Homethis.$router.push({
              path: '/login',
              query: {
                from: '/home'
              }
            })
          }
        })
        return false
      }
      // 如果登录了， 加入购物车
      addCart({
        goodsId: id,
        number: num,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          Toast.success('加入成功')
          state.Homethis.show = false
          // this.commit('cart/set_items', res.data.data.items)
        }
      })
    }
  }
}
